<?xml version="1.0" encoding="gb2312"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:output method="text" />
  <xsl:param name="languageCode" />
  <!--
	if a label does not exist, which language to use as the default
-->
  <xsl:variable name="defaultLanguageCode">1033</xsl:variable>
  <xsl:template match="/preview"><![CDATA[<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" xmlns:crm="http://mscrm" version="1.0" exclude-result-prefixes="msxsl crm">]]><![CDATA[<xsl:output method="html" indent="no"/>]]><![CDATA[<xsl:param name="numberFormat"/>]]><![CDATA[<xsl:param name="numberFormatMask"/>]]><![CDATA[<xsl:param name="currencySymbol"/>]]><!--
		set the number format to use for format-number() later
	--><![CDATA[<xsl:decimal-format name="us" grouping-separator="," decimal-separator="."/>]]><![CDATA[<xsl:decimal-format name="eu" grouping-separator="." decimal-separator=","/>]]><![CDATA[<xsl:decimal-format name="fr" grouping-separator=" " decimal-separator=","/>]]><![CDATA[<xsl:decimal-format name="nz" grouping-separator="'" decimal-separator="."/>]]><!--
		helper function used in the number formatting process
	--><![CDATA[<xsl:template name="replace">]]><![CDATA[<xsl:param name="source" />]]><![CDATA[<xsl:param name="oldVal" />]]><![CDATA[<xsl:param name="newVal" />]]><![CDATA[<xsl:variable name="temp" select="$source" />]]><![CDATA[<xsl:variable name="first" select="substring-before($temp, $oldVal)" />]]><![CDATA[<xsl:variable name="rest" select="substring-after($temp, $oldVal)" />]]><![CDATA[<xsl:value-of select="concat($first, $newVal)" />]]><![CDATA[<xsl:choose>]]><![CDATA[<xsl:when test="(substring-before($rest, $oldVal)) or (substring-after($rest, $oldVal))">]]><![CDATA[<xsl:call-template name="replace">]]><![CDATA[<xsl:with-param name="source" select="$rest" />]]><![CDATA[<xsl:with-param name="oldVal" select="$oldVal" />]]><![CDATA[<xsl:with-param name="newVal" select="$newVal" />]]><![CDATA[</xsl:call-template>]]><![CDATA[</xsl:when>]]><![CDATA[<xsl:otherwise>]]><![CDATA[<xsl:value-of select="$rest" />]]><![CDATA[</xsl:otherwise>]]><![CDATA[</xsl:choose>]]><![CDATA[</xsl:template>]]><![CDATA[<xsl:template match="/]]><xsl:value-of select="@name" /><![CDATA[">]]><![CDATA[<table id="gridPreview" cellspacing="0" class="gridPreview]]><xsl:value-of select="@object" /><![CDATA[">]]><![CDATA[<col class="gridPreviewLabel"/>]]><![CDATA[<col class="gridPreviewData"/>]]><![CDATA[<col width="10"/>]]><![CDATA[<col class="gridPreviewLabel"/>]]><![CDATA[<col class="gridPreviewData"/>]]><xsl:for-each select="rows/row"><![CDATA[<tr style="padding:5px;">]]><xsl:choose><!--
					cell spans both columns
				--><xsl:when test="not(cell[2])"><xsl:apply-templates select="cell[1]"><xsl:with-param name="colspan">true</xsl:with-param></xsl:apply-templates></xsl:when><xsl:otherwise><xsl:choose><xsl:when test="cell[1]/@name"><xsl:apply-templates select="cell[1]" /><![CDATA[<td style="border-bottom:0px;"><br/></td>]]></xsl:when><xsl:otherwise><![CDATA[<td><br/></td>]]><![CDATA[<td><br/></td>]]><![CDATA[<td><br/></td>]]></xsl:otherwise></xsl:choose><xsl:choose><xsl:when test="cell[2]/@name"><xsl:apply-templates select="cell[2]" /></xsl:when><xsl:otherwise><![CDATA[<td><br/></td>]]><![CDATA[<td><br/></td>]]></xsl:otherwise></xsl:choose></xsl:otherwise></xsl:choose><![CDATA[</tr>]]></xsl:for-each><![CDATA[</table>]]><![CDATA[</xsl:template>]]><![CDATA[</xsl:stylesheet>]]></xsl:template>
  <!--
	writes out the cell
-->
  <xsl:template match="/preview/rows/row/cell">
    <!--
		does this cell span multiple columns?
	-->
    <xsl:param name="colspan" />
    <!--
		populate local variables
	-->
    <xsl:variable name="fieldName" select="@name" />
    <xsl:variable name="dataType" select="/preview/metadata/fields/field[@name = $fieldName]/@datatype" />
    <xsl:variable name="defaultValue" select="/preview/properties/fields/field[@name = $fieldName]/@defaultvalue" />
    <xsl:variable name="showGrouping" select="/preview/properties/fields/field[@name = $fieldName]/@grouping" />
    <xsl:variable name="decAccuracy" select="/preview/properties/fields/field[@name = $fieldName]/@accuracy" />
    <xsl:variable name="format" select="/preview/properties/fields/field[@name = $fieldName]/@format" />
    <!--
		writes out the cell label
	--><![CDATA[<td>]]><xsl:choose><xsl:when test="/preview/properties/fields/field[@name = $fieldName]/displaynames/displayname[@languagecode = $languageCode]"><xsl:value-of select="/preview/properties/fields/field[@name = $fieldName]/displaynames/displayname[@languagecode = $languageCode]/@description" /></xsl:when><xsl:otherwise><xsl:value-of select="/preview/properties/fields/field[@name = $fieldName]/displaynames/displayname[@languagecode = $defaultLanguageCode]/@description" /></xsl:otherwise></xsl:choose><xsl:choose><xsl:when test="$dataType = 'money'"><![CDATA[ (<xsl:value-of select="$currencySymbol"/>)]]></xsl:when></xsl:choose><![CDATA[ :</td>]]><![CDATA[<td]]><xsl:if test="$colspan"> colspan="4"</xsl:if><![CDATA[>]]><xsl:choose><!--
				boolean and picklist datatypes
			--><xsl:when test="$dataType = 'boolean' or $dataType = 'picklist' or $dataType = 'state' or $dataType = 'status'"><![CDATA[<xsl:value-of select="]]><xsl:value-of select="$fieldName" /><![CDATA[/@name"/>]]></xsl:when><!--
				lookup datatype
			--><xsl:when test="$dataType = 'lookup' or $dataType = 'owner' or $dataType = 'customer'"><![CDATA[<xsl:if test="]]><xsl:value-of select="$fieldName" /><![CDATA[/@dsc = 0">]]><![CDATA[<xsl:value-of select="]]><xsl:value-of select="$fieldName" /><![CDATA[/@name"/>]]><![CDATA[</xsl:if>]]></xsl:when><!--
			integer, float, and money datatypes
		--><xsl:when test="$dataType = 'integer' or $dataType = 'float' or $dataType = 'money'"><![CDATA[<xsl:if test="]]><xsl:value-of select="$fieldName" /><![CDATA[">]]><xsl:if test="$dataType = 'money'"><![CDATA[<xsl:value-of select="concat($currencySymbol, ' ')"/>]]></xsl:if><!--
						numberFormatMask has the following structure "#,##0.*;0#,##0.*"
						We need to replace '*'s with the appropriate number of 0s to specify the accuracy
						For example if the accuracy is three the formatNumberPattern variable will have the value of "#,##0.000;0#,##0.000"
						If the accuracy iz zero (i.e. the number doesn't have antyhing after the decimal point)
						we convert the mask to the following form: "#,##0.#;0#,##0.#"
					--><![CDATA[<xsl:variable name="formatNumberPattern">]]><xsl:choose><xsl:when test="$decAccuracy&gt;0"><![CDATA[<xsl:call-template name="replace">]]><![CDATA[<xsl:with-param name="source" select="$numberFormatMask" />]]><![CDATA[<xsl:with-param name="oldVal" select="'*'" />]]><![CDATA[<xsl:with-param name="newVal" select="']]><xsl:value-of select="substring('0000000000', 1, $decAccuracy)" /><![CDATA['" />]]><![CDATA[</xsl:call-template>]]></xsl:when><xsl:otherwise><![CDATA[<xsl:call-template name="replace">]]><![CDATA[<xsl:with-param name="source" select="$numberFormatMask" />]]><![CDATA[<xsl:with-param name="oldVal" select="'*'" />]]><![CDATA[<xsl:with-param name="newVal" select="'#'" />]]><![CDATA[</xsl:call-template>]]></xsl:otherwise></xsl:choose><![CDATA[</xsl:variable>]]><![CDATA[<xsl:variable name="formattedNumber" select="format-number(]]><xsl:value-of select="$fieldName" /><![CDATA[, $formatNumberPattern, $numberFormat)"/>]]><!--
						replace	'~' sign with the '-' sign
						'~' is used to deal with some strange behavior of the XSLT processor when '-' is used in the pattern
					--><![CDATA[<xsl:value-of select="translate($formattedNumber, '~', '-')"/>]]><![CDATA[</xsl:if>]]></xsl:when><!--
			datetime datatype
		--><xsl:when test="$dataType = 'datetime'"><![CDATA[<xsl:value-of select="]]><xsl:value-of select="$fieldName" /><![CDATA[/@date"/>]]><xsl:if test="$format = 'datetime'"><![CDATA[<xsl:text disable-output-escaping="yes">&amp;nbsp;</xsl:text><xsl:value-of select="]]><xsl:value-of select="$fieldName" /><![CDATA[/@time"/>]]></xsl:if></xsl:when><!--
				memo and text datatypes
			--><xsl:when test="$dataType = 'memo' or $dataType = 'text'"><![CDATA[<xsl:value-of select="]]><xsl:value-of select="$fieldName" /><![CDATA[" disable-output-escaping="yes"/>]]></xsl:when><!--
				uniqueidentifier
			--><xsl:when test="$dataType = 'uniqueidentifier'"><![CDATA[<xsl:if test="]]><xsl:value-of select="$fieldName" /><![CDATA[/@dsc = 0">]]><![CDATA[<xsl:value-of select="]]><xsl:value-of select="$fieldName" /><![CDATA[/@name"/>]]><![CDATA[</xsl:if>]]></xsl:when><!--
				not a recognized datatype
			--><xsl:otherwise><xsl:value-of select="$fieldName" /></xsl:otherwise></xsl:choose><![CDATA[<br/>]]><![CDATA[</td>]]></xsl:template>
</xsl:stylesheet>